package com.cdc.api;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class MobileDataAction extends ActionSupport{
	HttpServletRequest request;
	HttpServletResponse response;
	ServletContext con;
	private static final long serialVersionUID = -3139539662323766165L;
	/***
	 * 通过ServletActionContext获得Servlet Api
	 */
	private void init() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		con = ServletActionContext.getServletContext();
		response.setCharacterEncoding("UTF-8");
	}
	
	public String getUser() throws Exception{
		init();
		String param1 = request.getParameter("name");
		String param2 = request.getParameter("password");
		if (param1 == null || param2 == null) {
			jsonUser("cdc", "123");
		} else {
			jsonUser("param1", "param2");
		}
		return NONE;
	}
	
	private void jsonUser(String name,String password) throws IOException {
		PrintWriter p = response.getWriter();
		String urlPath="https://cdcdec.github.io/images/%E5%A4%B4%E5%83%8F%E5%A4%A7%E7%99%BD_300.jpg";
		User user = new User(name,password,urlPath);
		Gson gson = new Gson();
		p.print(gson.toJson(user));
	}
	/**只能返回null
	 * 默认的Action
	 * @return
	 * @throws Exception
	 */
	public String defaultAction() throws Exception{
		init();
		PrintWriter p = response.getWriter();
		String back="can't find this action";
		p.print(back);
		return NONE;
	}
	
	
}
